package lumingweihua.future.cn.lumingweihua.network;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zhusx.core.interfaces.IHttpResult;
import com.zhusx.core.network.HttpRequest;

import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;
import lumingweihua.future.cn.lumingweihua.R;

/**
 * Created by z1986 on 2018/3/18.
 */

public abstract class LoadingHelper<T> extends LoadingBaseHelper<LoadData.Api, T, Object> {
    PtrClassicFrameLayout frameLayout;

    public LoadingHelper(final View resView, final LoadData<T> loadData) {
        super(resView);
        View view = LayoutInflater.from(resView.getContext()).inflate(R.layout.layout_helper, null, false);
        //设置Loading 视图
        View loadingView = view.findViewById(R.id.layout_loading);
        _setLoadingView(loadingView);
        View errorView = view.findViewById(R.id.layout_error);
        //设置 失败后,重试
        errorView.findViewById(R.id.tv_reload).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadData._reLoadData();
            }
        });
        //设置失败 视图
        _setErrorView(errorView);
        if (resView instanceof RecyclerView) {
            View emptyView = view.findViewById(R.id.layout_empty);
            switch (loadData._getRequestID()) {
                case 个人中心我的车辆列表:
                    ((TextView) emptyView.findViewById(R.id.layout_empty)).setText("暂无车辆，请立即添加");
                    break;
            }
            _setEmptyView(emptyView);
            frameLayout = new RefreshLayout((ViewGroup) resView.getParent());
            frameLayout.setPtrHandler(new PtrHandler() {
                @Override
                public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                    return PtrDefaultHandler.checkContentCanBePulledDown(frame, resView, header);
                }

                @Override
                public void onRefreshBegin(PtrFrameLayout frame) {
                    if (loadData._isLoading()) {
                        frameLayout.refreshComplete();
                        return;
                    }
                    loadData._reLoadData(true);
                }
            });
        }
    }

    @Override
    public void onLoadError(LoadData.Api api, HttpRequest<Object> request, IHttpResult<T> data, boolean isAPIError, String error_message) {
        super.onLoadError(api, request, data, isAPIError, error_message);
        if (frameLayout != null && frameLayout.isRefreshing()) {
            frameLayout.refreshComplete();
        }
    }

    @Override
    public void onLoadComplete(LoadData.Api api, HttpRequest<Object> request, IHttpResult<T> data) {
        super.onLoadComplete(api, request, data);
        if (frameLayout != null && frameLayout.isRefreshing()) {
            frameLayout.refreshComplete();
        }
    }

    @Override
    public void __onError(View errorView, HttpRequest<Object> request, IHttpResult<T> data, boolean isAPIError, String error_message) {
        if (!TextUtils.isEmpty(error_message)) {
            ((TextView) errorView.findViewById(R.id.tv_error)).setText(error_message);
        }
    }

    @Override
    public boolean isShowLoading() {
        if (frameLayout != null && frameLayout.isRefreshing()) {
            return false;
        }
        return super.isShowLoading();
    }
}
